package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.QiniuUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {
   @Reference
   private MemberService memberService;

   @RequestMapping("/getMemberStatistics")
   public Result getMemberStatistics(@RequestBody Map<String,String> map) {
      try {
         Map<String,Object> result = memberService.getMemberStatistics(map);
         return new Result(true, MessageConstant.GET_MEMBER_STATISTICS_SUCCESS, result);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false, MessageConstant.GET_MEMBER_STATISTICS_FAIL);
      }
   }
    @RequestMapping("/add")
    public Result add (@RequestBody Member member){
            try{
                Date regTime = DateUtils.getToday();
                member.setRegTime(regTime);
                memberService.add(member);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
            }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //��ҳ��ѯ
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        //����memberService����findPage����
        PageResult pageResult = memberService.findPage(pageBean);
        return pageResult;
    }
    //���ݵ绰���һ�Ա
    @RequestMapping("/findByTelephone/{phoneNumber}")
    public Result findByTelephone (@PathVariable String phoneNumber){
        try{
            Member member = memberService.findByTelephone(phoneNumber);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
    //���»�Ա��Ϣ
    @RequestMapping("/update")
    public Result update (@RequestBody Member member){
        try{
            memberService.update(member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     *
     * 体检上传
     * @param queryPageBean
     * @return
     */
    //    分页查询
    @RequestMapping("/findPageBySC")
    public PageResult findPageBySC(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageBySC = memberService.findPageBySC(queryPageBean);
        return pageBySC;
    }


    //删除
    @RequestMapping("/delete/{phoneNumber}")
    public Result delete(@PathVariable String phoneNumber) {
        try {
            Map map = memberService.deleteByPhoeNumber(phoneNumber);
            //获取文件名
            for (Object key : map.keySet()) {
                String fileName = (String) map.get(key);
                //删除七牛云存储的文件
                QiniuUtils.deleteFileFromQiniu(fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    //上传文件
    @RequestMapping("/upload")
    public Result upload(MultipartFile file, HttpServletRequest request) {
        try {
            String fileName = file.getOriginalFilename();//原始名字
            //文件上传
            QiniuUtils.upload2Qiniu(file.getBytes(), fileName);
            //将文件名存入Session
            request.getSession().setAttribute("fileName", fileName);
            return new Result(true, MessageConstant.DOC_UPLOAD_SUCCESS);//上传成功
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DOC_UPLOAD_SUCCESS);//上传失败
        }
    }

    //文件上传成功，修改会员报告状态
    @RequestMapping("/updateByType/{phoneNumber}")
    public Result updateByType(@PathVariable String phoneNumber, HttpServletRequest request) {
        try {
            //从session获取数据
            String fileName = (String) request.getSession().getAttribute("fileName");
            System.out.println(fileName);
            memberService.setFileName(phoneNumber, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_TYPE_FAIL);
        }
        return new Result(true, MessageConstant.UPDATE_TYPE_SUCCESS);

    }
}
